package top.jnaw.jee.utils;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import top.jnaw.jee.platform.model._MappingKit;

public class TestUnits {

  private ActiveRecordPlugin arp;
  private DruidPlugin druid;
  private IPlugin cache;

  public TestUnits(String propertyFile) {
    PropKit.use(propertyFile);
  }

  public TestUnits() {
    PropKit.use("developer.properties");
  }

  public boolean startOrmTest() {
    Log.init("ORM.TEST");

    druid = (DruidPlugin) Generator.dataSourcePlugin();
    arp = new ActiveRecordPlugin(druid);
    arp.setShowSql(PropKit.getBoolean("show_sql"));
    arp.setCache(new RedisCache());

    Models.addSqlTemplate(arp);
    _MappingKit.mapping(arp);
    return arp.start();
  }

  public boolean stopOrmTest() {
    boolean result = false;
//    if (null != arp) {
//      arp.stop();
//      result = true;
//    }

    if (null != druid) {
      druid.stop();
    }

    return result;
  }

  public boolean startCacheTest() {
    boolean result = false;

    cache = Generator.cachePlugin();
    cache.start();

    return Cache.init();
  }

  public boolean stopCacheTest() {
    boolean result = false;

    if (null != cache) {
      result = cache.stop();
    }

    return result;
  }


}
